
/*-----------------------------------------------------------------------------\
|                            The Pomodoro technique                            |
|                                                                              |
| Developer: Felipe Simões Lage Gomes Duarte                                   |
|                                                                              |
| Main Project: The Pomodoro technique                                         |
| Date: 28 / maio / 2011                                                       |
| Code: InstanceControl.java                                                   |
| IDE: NetBeans IDE 7.0 (Build 201104080000)                                   |
| Java: 1.6.0_24; Java HotSpot(TM) Client VM 19.1-b02                          |
|                                                                              |
\-----------------------------------------------------------------------------*/

package util;

import interfaces.Log;
import interfaces.SystemTrayInterface;
import java.awt.TrayIcon.MessageType;
import java.io.*;
import java.net.*;

public class InstanceControl extends Thread {

   private ServerSocket ssocket = null;
   protected Socket csocket = null;
   private int port = 23734;
   private SystemTrayInterface sti;
   private boolean live;
   private final int codigo = 762537812;

   public InstanceControl(SystemTrayInterface sti) {
      this.sti = sti;
   }

   public void kill() {
      try {
         live = false;
         if (ssocket != null) {
            ssocket.close();
         }
         if (csocket != null) {
            csocket.close();
         }
      } catch (IOException ex) {
         String error = "Couldn't open gate: " + Integer.toString(port) + "\n";
         StackTraceElement[] st = ex.getStackTrace();
         for (int i = 0; i < st.length; i++) {
            error += st[i].toString() + "\n";
         }
         Log.fatal(error);
      }
   }

   @Override
   public void run() {
      try {
         live = true;
         ssocket = new ServerSocket(port);
      } catch (Exception ex) {
         try {
            csocket = new Socket("127.0.0.1", port);
            ObjectOutputStream out = new ObjectOutputStream(csocket.getOutputStream());
            out.writeObject(new Integer(codigo));
         } catch (Exception ex1) {
            String error = null;
            StackTraceElement[] st = ex1.getStackTrace();
            for (int i = 0; i < st.length; i++) {
               error += st[i].toString() + "\n";
            }
            Log.fatal(error);
         } finally {
            sti.fechar();
         }
      }
      while (live) {
         try {
            csocket = ssocket.accept();
            ObjectInputStream in = new ObjectInputStream(csocket.getInputStream());
            Integer i = ((Integer) in.readObject());
            if (i.equals(new Integer(codigo))) {
               SystemTrayInterface.showMessage("Another instance is alredy Running", MessageType.WARNING);
            }
         } catch (Exception ex1) {
            String error = null;
            StackTraceElement[] st = ex1.getStackTrace();
            for (int i = 0; i < st.length; i++) {
               error += st[i].toString() + "\n";
            }
            Log.fatal(error);
         }
      }
   }
}
